package controllers

import (
	"github.com/gin-gonic/gin"
	"wakitsoft.com/resume/db"
	"wakitsoft.com/resume/libs"
	"wakitsoft.com/resume/models"
)

func GetTechStacks(c *gin.Context) {
	var stacks []models.TechStack
	db.GetDB().Model(&models.TechStack{}).Find(&stacks)
	libs.Success(c, stacks)
}

type createTechStackRequest struct {
	Name  string `json:"name" binding:"required"`
	Style string `json:"style" binding:"required"`
}

func CreateTechStack(c *gin.Context) {
	var req createTechStackRequest
	if libs.ValidateRequest[createTechStackRequest](c, &req) == false {
		return
	}
	var stack = &models.TechStack{
		Style: req.Style,
		Name:  req.Name,
	}

	db.GetDB().Model(&models.TechStack{}).Create(stack)
	libs.Success(c, stack)
}

func DeleteTechStack(c *gin.Context) {
	id := c.Param("id")
	var stack models.TechStack
	db.GetDB().Model(&models.TechStack{}).Where("id = ?", id).First(&stack)
	db.GetDB().Model(&models.TechStack{}).Delete(&stack)
	libs.Success(c, stack)
}

func UpdateTechStack(c *gin.Context) {
	var req createTechStackRequest
	if libs.ValidateRequest[createTechStackRequest](c, &req) == false {
		return
	}
	id := c.Param("id")
	var stack models.TechStack
	db.GetDB().Model(&models.TechStack{}).Where("id = ?", id).First(&stack)
	if req.Name != "" {
		stack.Name = req.Name
	}

	if req.Style != "" {
		stack.Style = req.Style
	}
	db.GetDB().Save(&stack)
	libs.Success(c, stack)
}
